package org.kernely.security.ui.group.mgmt;

import org.kernely.ui.mvp.IKernelyView;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class AddEditGroupManagementView extends VerticalLayout implements IKernelyView{

	/**
	 * The Serial Version ID
	 */
	private static final long serialVersionUID = 249759751921674080L;
	
	private TextField name;
	private HorizontalLayout addLayout;
	private ComboBox usersBox; // Where you can choose a user
	private ListSelect usersList; // Where group's users are displayed
	private Label usersLabel;
	private Button addButton;
	private Button deleteButton;
	private final int WIDTH = 310;
	
	/**
	 * AddEditGroupManagementView's constructor
	 */
	public AddEditGroupManagementView(){
		this.name = new TextField("Name");
		this.name.setWidth(WIDTH, Sizeable.UNITS_PIXELS);
		this.name.setWidth(WIDTH, Sizeable.UNITS_PIXELS);
		this.name.setRequired(true);
		this.name.setRequiredError("Name can not be empty!");
		
		usersLabel = new Label("Users");
		
		// The combo box containing selectable users
		usersBox = new ComboBox();
		usersBox.setImmediate(true);
		usersBox.setNullSelectionAllowed(false);
		usersBox.setInputPrompt("Please select a user");
		usersBox.setWidth(225, Sizeable.UNITS_PIXELS);
		usersBox.setFilteringMode(AbstractSelect.Filtering.FILTERINGMODE_CONTAINS);

		// The list containing users in the group
		usersList = new ListSelect();
		usersList.setImmediate(true);
		usersList.setNullSelectionAllowed(false);
		usersList.setWidth(WIDTH, Sizeable.UNITS_PIXELS);
		usersList.setMultiSelect(true);

		addButton =  new Button("Add");
		addButton.setWidth(80,UNITS_PIXELS);
		deleteButton = new Button("Delete");
		addButton.setEnabled(false);
		deleteButton.setEnabled(false);
		addLayout = new HorizontalLayout();
		addLayout.addComponent(usersBox);
		addLayout.addComponent(addButton);
		
		this.addComponent(this.name);
		this.addComponent(usersLabel);
		this.addComponent(this.addLayout);
		this.addComponent(usersList);
		this.addComponent(this.deleteButton);
		
		// Basic behaviour of the buttons
		this.usersList.addListener(new Property.ValueChangeListener() {
			private static final long serialVersionUID = -5368063708649993389L;

			/**
			 * The method called when a user select an item in the list.
			 */
			@Override
			public void valueChange(ValueChangeEvent event) {
				if (usersList.getValue() == null){
					deleteButton.setEnabled(false);
				}
				else {
					deleteButton.setEnabled(true);
				}
			}
		});
		this.usersBox.addListener(new Property.ValueChangeListener() {
			private static final long serialVersionUID = -5368063708649993389L;

			/**
			 * The method called when a user select an item in the list.
			 */
			@Override
			public void valueChange(ValueChangeEvent event) {
				if (usersBox.getValue() == null){
					addButton.setEnabled(false);
				}
				else {
					addButton.setEnabled(true);
				}
			}
		});
		this.addLayout.setSpacing(true);
		this.name.focus();
		this.setWidth(350, Sizeable.UNITS_PIXELS);
		this.setMargin(true,true,true,true);
		this.setSpacing(true);
	}

	/**
	 * @see org.kernely.ui.mvp.IKernelyView#getComponent()
	 */
	public final Component getComponent() {
		return this;
	}

	/**
	 * @return the name
	 */
	public final TextField getName() {
		return name;
	}

	/**
	 * Gets the usersBox
	 * @return the usersBox
	 */
	public final ComboBox getUsersBox() {
		return usersBox;
	}

	/**
	 * Gets the usersList
	 * @return the usersList
	 */
	public final ListSelect getUsersList() {
		return usersList;
	}

	/**
	 * Gets the addButton
	 * @return the addButton
	 */
	public final Button getAddButton() {
		return addButton;
	}

	/**
	 * Gets the deleteButton
	 * @return the deleteButton
	 */
	public final Button getDeleteButton() {
		return deleteButton;
	}
}
